TC_DATA_CONFIG = {
    TYPE:'',
    BINDING_URI: base_url + 'cms/cp/data/bindingbytype/',
    EDITPANEL_URI:base_url + 'cms/cp/data/editpanel/',
    COMMIT_URI:base_url + 'cms/cp/data/oncommit/',
    DELETE_URI:base_url + 'cms/cp/data/ondelete/'
};
var TC_DATA = (function() {
	
    var oTable;
    var that = this;
    return {
        onCommit: function(Url,Params, Id,callback) {
            /* Commit data l�n v�o DB, Id>0 l� Update, Id=0 ho?c ko c� l� Insert */
            backend({
                url: Url,
                data: {
                    Id: Id,
                    Params: Params
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {
                            sticky: true, 
                            theme: 'growl-error', 
                            header: 'Error!'
                        });
                    } else {
                        $.jGrowl(rsdata.message);
                        if (typeof callback == 'function') {
                            callback(rsdata);
                        }
                    }
                }
            }).call();
        },
        createtable :   function(){
            oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [
                {
                    mData: "data_id",
                    sWidth: "40px",
                    sClass:'gridAction',
                    mRender: function ( value, type, datarow ) {
                        var str = 
                        '<ul class="table-controls">'+
                        //'<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
                        '<li><a href="#" class="tip" onclick="TC_DATA.onEditItem(' + value + ')" title="Edit entry" ><i class="icon-pencil"></i></a> </li>'+
                        '<li><a href="#" class="tip" onclick="TC_DATA.onDeleteItem(' + value + ')" title="Remove entry" ><i class="icon-trash"></i></a> </li>'+
                        '</ul>';
                        return str;
                    }
                },
                {
                    mData: "data_source",
                    sWidth: "40px",
                    sClass:'gridThumb',
                    mRender: function ( value, type, datarow ) {
                        if((!value || /^\s*$/.test(value))) return '';
                        if(value.startsWith('/data/image/'))value=value.replace('/data/image/','/data/thumbs/image/');
                        return '<img src="' + value + '" title="Thumb"/>';
                            
                    }
                },
                {
                    mData: "data_title"
                },
                {
                    mData: "data_position",
                    sWidth: "60px"
                },
                {
                    mData: "data_status",
                    sWidth: "60px",
                    sClass:'gridStatus',
                    mRender: function ( value, type, datarow ) {
                        var str=
                        '<div>'+
                        '<div onclick="TC_DATA.onStatusChange(this,'+datarow.data_id+')" class="icheckbox'+(value==="true"?" checked":"")+'"></div>'+
                        '<span>'+(value==="true"?"Enable":"Disable")+'</span>'
                        '</div>';
                        return str;
                    }
                },
                {
                    mData: "data_insert",
                    sWidth: "120px"
                },

                {
                    mData: "data_update",
                    sWidth: "120px"
                }
                ],
                sServerMethod: "POST",
                iDisplayLength: 10,
                bProcessing: true,
                bServerSide: true,
                bPaginate: false,
                sAjaxSource: TC_DATA_CONFIG.BINDING_URI+TC_DATA_CONFIG.TYPE,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {
                        "sFirst": "First", 
                        "sLast": "Last", 
                        "sNext": ">", 
                        "sPrevious": "<"
                    }
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit  :   function(){
            this.createtable();
        },
        onAddItem:function(){
            this.onEditItem(0);
        },
        onEditItem:function(Id){
            if (PENDING)
                return false;
            $('#dialog-edit-panel>.base-dialog-modal').html('<div class="base_loading" style="display: block;height: 60px"></div>');
            bckdialog({
                title:'Add/Edit Data',
                message:$('#dialog-edit-panel'),
                buttons: [
                {
                    text    :'Done',
                    class   :'base_button base_green base-small-border-radius',
                    click   : function() {
                        TC_DATA.onSave();
                    }
                },
                {
                    text    :'Close',
                    class   :'base_button base_red base-small-border-radius',
                    click   : function() {
                        $(this).dialog("close");
                    }
                }
                ]
            }).open();
            backend({
                url         :   TC_DATA_CONFIG.EDITPANEL_URI+TC_DATA_CONFIG.TYPE,
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        $('#dialog-edit-panel>.base-dialog-modal').html('<span style="color:red">'+rsdata.message+'</span>');
                    }else{
                        $('#dialog-edit-panel>.base-dialog-modal').html(rsdata.htmlreponse);
                        $('#dialog-edit-panel').dialog('close').dialog('open'); 
                        $('#_form_edit_panel select').select2({
                            minimumResultsForSearch :-1
                        });
                    }
                }
            }).call();
        },
        onSave:function(){
            if( $('#_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#_form_edit_panel').serializeObject();
            $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').show();
            this.onCommit(TC_DATA_CONFIG.COMMIT_URI,Params,$('#data_id').val(),function(rsdata){
                $("#dialog-edit-panel").parents('.ui-dialog').find('.dialog-pending').hide();
                if(rsdata.result<0){
                    bckdialog({
                        type:'error'
                    }).open(rsdata.message);
                }else{
                    TC_DATA.onRefresh();
                    TC_DATA.onCancel();
                }
            });
            return false;
        },
        onCancel:function(){
            $('#dialog-edit-panel').dialog('close');
        },
        onDeleteItem:function(Id){
            backend_delete(TC_DATA_CONFIG.DELETE_URI,Id,function(){
                TC_DATA.onRefresh();
            });
        },
        onRefresh:function(){
            oTable.fnDraw(false);
        },
        onFillter:function(v){
            oTable.fnFilter(v, 2);
        },
        onStatusChange:function(that,Id){
            var status = $(that).hasClass('checked')?'false':'true';
            this.onCommit( TC_DATA_CONFIG.COMMIT_URI, {
                data_status:status
            }, Id, function(rsdata){
                if(rsdata.result<0){
                    bckdialog({
                        type:'error'
                    }).open(rsdata.message);
                }else{
                    $(that).toggleClass('checked');
                    $(that).next().html(status=='false'?'Disable':'Enable')
                }
            });
        },
        onChangeSource : function(type){
            if(type!==TC_DATA_CONFIG.TYPE){
                TC_DATA_CONFIG.TYPE=type;
                oTable.fnSettings().sAjaxSource = (TC_DATA_CONFIG.BINDING_URI+TC_DATA_CONFIG.TYPE);
                oTable.fnDraw(false);
            }
        }
    };
}());